package org.ReportDesigner.designerXML;

import org.ReportDesigner.Designer;
import org.ReportDesigner.reportModel.ReportElement;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;

/**
 * класс отвечает за построение дерева XML документа,
 * которое передается PDFDesigner, создающему PDF документ
 * использует SAX парсер XML документо для чтения и парсинга
 * XML документа.
 */
public class XMLDesigner implements Designer {

    private static final Logger log = Logger.getLogger(XMLDesigner.class);

    private File inputFile;
    private File outputFile;
    private ReportElement report;
    public XMLDesigner(File outputFile, File inputFile) {
        this.outputFile = outputFile;
        this.inputFile = inputFile;
    }

    public XMLDesigner(File inputFile) {
        this.inputFile = inputFile;
    }
     public XMLDesigner(){

     }


    @Override
    public void design() {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = saxParserFactory.newSAXParser();
            EventHandler handler = new EventHandler();
            handler.setReport(report);
            saxParser.parse(inputFile, handler);
            report = handler.getReport();

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        EventHandler eventHandler = new EventHandler();
    }


    public ReportElement getResult() {
        return report;
    }

    public void setOutputFile(File outputFile) {
        this.outputFile = outputFile;
    }

    public File getInputFile() {
        return inputFile;
    }

    public void setInputFile(File inputFile) {
        this.inputFile = inputFile;
    }

    public ReportElement getReport() {
        return report;
    }

    public void setReport(ReportElement report) {
        this.report = report;
    }
}
